//==================================================================
//  Copyright (C) 2006  Davide Pasca
//
//  This library is free software; you can redistribute it and/or
//  modify it under the terms of the GNU Lesser General Public
//  License as published by the Free Software Foundation; either
//  version 2.1 of the License, or (at your option) any later version.
//
//  This library is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
//  Lesser General Public License for more details.
//
//  You should have received a copy of the GNU Lesser General Public
//  License along with this library; if not, write to the Free Software
//  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
//==================================================================
/// @file m5d_displist_vpool.h
///
///
///
//==================================================================

#ifndef M5D_DISPLIST_VPOOL_H
#define M5D_DISPLIST_VPOOL_H

#include "pvector.h"
#include "m5d_types.h"
#include "m5d_verts.h"

//==================================================================
namespace M5D
{

//==================================================================
/// DListVertPool
//==================================================================
class DListVertPool : public PSYS::sync_obj
{
public:
	//==================================================================
	enum
	{
		FORMAT_EMPTY	= 0,
		FORMAT_FLOAT	= 1,
		FORMAT_BYTE		= 2,
		FORMAT_N
	};

	int		_n_verts;
	void	*_datap[VType::N];
	int		_byte_offs[VType::N];
	u_int	_formats[VType::N];
	u_int	_sizes[VType::N];
	u_int	_stride;
	u_int	_size;

	static const int	_vformat_size_table[FORMAT_N];
	static const int	_vert_count_table[VType::N];

public:
	//==================================================================
	DListVertPool()
	{
		Reset();
		_datap[0] = NULL;
	}
	//==================================================================
	DListVertPool( const DListVertPool *src_objp );

	//==================================================================
	~DListVertPool()
	{
		FreeData();
	}

	//==================================================================
	static u_int CalcVertSize( int vusage, int format )
	{
		return _vformat_size_table[ format ] * _vert_count_table[ vusage ];
	}

	//==================================================================
	void	Reset();
	void	AddItem( u_int format, VType::VType usage );
	void	AllocateData( int n_verts ) throw(...);
	void	FreeData();
	void	CopyVert( int vidx, VType::VType usage, const void *src_datap, u_int src_format );
	bool	IsActiveUsage( VType::VType usage ) const
	{
		return _formats[ usage ] != FORMAT_EMPTY;
	}

	const u_char	*GetVertPtr( int vidx, VType::VType usage ) const
	{
		return (u_char *)_datap[usage] + _stride * vidx;
	}
	u_char	*GetVertPtr( int vidx, VType::VType usage )
	{
		return (u_char *)_datap[usage] + _stride * vidx;
	}

	void	CopyDefinitions( const DListVertPool &from );
};

};

#endif